#include "MyClient.h"
#include <iostream>


MyClient::MyClient(void)
{
}


MyClient::~MyClient(void)
{
}


MyTree MyClient::ConvertDirToTree(const MyString rootPath )
{
	MyTree myTree;// = new MyTree();
	try
	{
		path p((char*)rootPath);
		if(exists(p))
		{
			shared_ptr<MyNode> rootNode(new MyNode());
			if(is_directory(p))
			{
				MyFile* realDir = new MyDirectory();
				realDir->fileName = p.filename().generic_string();
				rootNode->containFile = realDir;
				//////////////////////////////////////////////MyDirectory
				ReadChildDir(p,rootNode);
			}
			else if (is_regular_file(p))
			{
				MyFile* file = new MyRealFile();
				file->fileName = p.filename().generic_string();
				rootNode->containFile = file;
				///////////////////////////////////////////////MyRealFile
			}
			myTree.root = rootNode;
		}
	}
	catch (const filesystem_error& ex)
	{
		std::cout << ex.what() << '\n';
	}
	
	return myTree;

}

void MyClient::PrintPreTree(MyTree* tree)
{
	
}
void MyClient::ReadChildDir(path p,shared_ptr<MyNode> parentNode)
{
	directory_iterator ite = directory_iterator(p);
	for(;ite != directory_iterator(); ++ite)
	{
		shared_ptr<MyNode> rootNode(new MyNode());
		if(is_directory(*ite))
		{
			MyFile* realDir = new MyDirectory();
			realDir->fileName = ite->path().generic_string();
			//std::cout << realDir->fileName << std::endl;
			rootNode->containFile = realDir;
			//////////////////////////////////////////////MyDirectory
			ReadChildDir(*ite,rootNode);
		}
		else if(is_regular_file(*ite))
		{
			MyFile* file = new MyRealFile();
			file->fileName = ite->path().generic_string();//filename().generic_string();
			//std::cout << file->fileName << std::endl;
			rootNode->containFile = file;
			///////////////////////////////////////////////MyRealFile
		}
		parentNode->childrenNodes.push_back(rootNode);
		MyString fileName(ite->path().generic_string());//filename().generic_string());
		//((MyDirectory*)(parentNode->containFile))->subFile.insert(fileName);
	}
}
